package longcaisuyun.longcai.com.yiliubabalongcaisuyun;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.amap.api.maps.AMap;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.overlay.DrivingRouteOverlay;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.route.BusRouteResult;
import com.amap.api.services.route.DrivePath;
import com.amap.api.services.route.DriveRouteResult;
import com.amap.api.services.route.RouteSearch;
import com.amap.api.services.route.WalkRouteResult;

import java.util.List;

import longcaisuyun.longcai.com.utils.AMapUtil;

//                            _ooOoo_
//                           o8888888o
//                           88" . "88
//                           (| -_- |)
//                            O\ = /O
//                        ____/`---'\____
//                      .   ' \\| |// `.
//                       / \\||| : |||// \
//                     / _||||| -:- |||||- \
//                       | | \\\ - /// | |
//                     | \_| ''\---/'' | |
//                      \ .-\__ `-` ___/-. /
//                   ___`. .' /--.--\ `. . __
//                ."" '< `.___\_<|>_/___.' >'"".
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |
//                 \ \ `-. \_ __\ /__ _/ .-` / /
//         ======`-.____`-.___\_____/___.-`____.-'======
//                            `=---='
//
//         .............................................
//         佛祖保佑             永无BUG
//         佛曰:
//                  写字楼里写字间，写字间里程序员；
//                  程序人员写程序，又拿程序换酒钱。
//                  酒醒只在网上坐，酒醉还来网下眠；
//                  酒醉酒醒日复日，网上网下年复年。
//                  但愿老死电脑间，不愿鞠躬老板前；
//                  奔驰宝马贵者趣，公交自行程序员。
//                  别人笑我忒疯癫，我笑自己命太贱；
//                  不见满街漂亮妹，哪个归得程序员？
/************************************************************/
//
/************************************************************/

/**
 * 导航页面
 */
public class LocationActivity extends Activity implements AMap.OnMapClickListener,
        AMap.OnMarkerClickListener, AMap.OnInfoWindowClickListener, AMap.InfoWindowAdapter,
        RouteSearch.OnRouteSearchListener, View.OnClickListener {

    private RouteSearch mRouteSearch;
    private DriveRouteResult mDriveRouteResult;
    private MapView mapView;
    private AMap aMap;
    private LatLonPoint mStartPoint = new LatLonPoint(45.75000, 126.63333);
    ;// 起点，
    private LatLonPoint mEndPoint1 = new LatLonPoint(43.88333, 125.35000);
    ;// 终点，
    private LatLonPoint mEndPoint2 = new LatLonPoint(30.51667, 114.31667);
    ;// 终点，
    private LatLonPoint mEndPoint3 = new LatLonPoint(34.76667, 113.65000);
    ;// 终点，
    private LatLonPoint mEndPoint4 = new LatLonPoint(Double.valueOf("45.75000"), Double.valueOf("45.75000"));
    ;// 终点，
    private LatLonPoint mEndPoint5;// 终点，
    LinearLayout fanhui_lin;
    String qijing = "";
    String qiwei = "";
    String zhongjing = "";
    String zhongwei = "";

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location);
        View view = getWindow().getDecorView();
        // 适配工具将视图重新排布
        MyApplication.ScaleScreenHelper.loadView((ViewGroup) view);
        mapView = (MapView) findViewById(R.id.route_map);
        mapView.onCreate(savedInstanceState);// 此方法必须重写
        Intent intent = getIntent();

        qijing = intent.getStringExtra("qijing");
        qiwei = intent.getStringExtra("qiwei");
        zhongjing = intent.getStringExtra("zhongjing");
        zhongwei = intent.getStringExtra("zhongwei");
//        哈尔滨经纬度:(126.63333,45.75000)<br>
//                长春经纬度:(125.35000,43.88333)<br>
//                武汉经纬度:(114.31667,30.51667)<br>
//                郑州经纬度:(113.65000,34.76667)<br>
//                石家庄经纬度:(114.48333,38.03333)<b

        init();
        setfromandtoMarker();
        searchRouteResult(2, RouteSearch.DrivingAvoidCongestion);
        LinearLayout fanhui_lin = (LinearLayout) findViewById(R.id.fanhui_lin);
        fanhui_lin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void setfromandtoMarker() {
        aMap.addMarker(new MarkerOptions()
                .position(AMapUtil.convertToLatLng(mStartPoint))
                .icon(BitmapDescriptorFactory.fromResource(R.mipmap.start)));
        aMap.addMarker(new MarkerOptions()
                .position(AMapUtil.convertToLatLng(mEndPoint1))
                .icon(BitmapDescriptorFactory.fromResource(R.mipmap.end)));
        aMap.addMarker(new MarkerOptions()
                .position(AMapUtil.convertToLatLng(mEndPoint2))
                .icon(BitmapDescriptorFactory.fromResource(R.mipmap.end)));
        aMap.addMarker(new MarkerOptions()
                .position(AMapUtil.convertToLatLng(mEndPoint3))
                .icon(BitmapDescriptorFactory.fromResource(R.mipmap.end)));
//        aMap.addMarker(new MarkerOptions()
//                .position(AMapUtil.convertToLatLng(mEndPoint4))
//                .icon(BitmapDescriptorFactory.fromResource(R.mipmap.end)));
    }

    private void init() {
        if (aMap == null) {
            aMap = mapView.getMap();
        }
        registerListener();
        mRouteSearch = new RouteSearch(this);
        mRouteSearch.setRouteSearchListener(this);
        fanhui_lin = (LinearLayout) findViewById(R.id.fanhui_lin);
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fanhui_lin:

                LocationActivity.this.finish();
                break;
        }
    }
    /**
     * 注册监听
     */
    private void registerListener() {
        aMap.setOnMapClickListener(LocationActivity.this);
        aMap.setOnMarkerClickListener(LocationActivity.this);
        aMap.setOnInfoWindowClickListener(LocationActivity.this);
        aMap.setInfoWindowAdapter(LocationActivity.this);
    }

    public View getInfoWindow(Marker marker) {
        return null;
    }

    @Override
    public View getInfoContents(Marker marker) {
        return null;
    }

    @Override
    public void onInfoWindowClick(Marker marker) {

    }

    @Override
    public void onMapClick(LatLng latLng) {

    }

    @Override
    public boolean onMarkerClick(Marker marker) {
        return false;
    }

    @Override
    public void onBusRouteSearched(BusRouteResult busRouteResult, int i) {

    }

    @Override
    public void onDriveRouteSearched(DriveRouteResult arg0, int arg1) {
        aMap.clear();// 清理地图上的所有覆盖物
        if (arg1 == 1000) {
            if (arg0 != null && arg0.getPaths() != null) {
                if (arg0.getPaths().size() > 0) {
                    mDriveRouteResult = arg0;
                    final DrivePath drivePath = mDriveRouteResult.getPaths()
                            .get(0);
                    List<Marker> list = aMap.getMapScreenMarkers();
                    Log.e("list", list.size() + "");
                    DrivingRouteOverlay drivingRouteOverlay = new DrivingRouteOverlay(
                            this, aMap, drivePath,
                            mDriveRouteResult.getStartPos(),
                            mDriveRouteResult.getTargetPos());
                    drivingRouteOverlay.removeFromMap();
                    drivingRouteOverlay.addToMap();
                    drivingRouteOverlay.zoomToSpan();
                    drivingRouteOverlay.setNodeIconVisibility(false);

                    int dis = (int) drivePath.getDistance();
                    int dur = (int) drivePath.getDuration();

//					mRotueTimeDes.setText(des);
//					mRouteDetailDes.setVisibility(View.VISIBLE);
//					int taxiCost = (int) mDriveRouteResult.getTaxiCost();
//					mRouteDetailDes.setText("打车约"+taxiCost+"元");

                } else if (arg0 != null && arg0.getPaths() == null) {
                    Toast.makeText(LocationActivity.this, "终点未设置", Toast.LENGTH_SHORT)
                            .show();
                }

            } else {
                Toast.makeText(LocationActivity.this, "终点未设置", Toast.LENGTH_SHORT)
                        .show();
            }
        } else {
            Toast.makeText(LocationActivity.this, "终点未设置", Toast.LENGTH_SHORT)
                    .show();
        }

    }

    @Override
    public void onWalkRouteSearched(WalkRouteResult walkRouteResult, int i) {

    }

    public void searchRouteResult(int routeType, int mode) {
        if (mStartPoint == null) {
            Toast.makeText(LocationActivity.this, "正在定位，请稍后", Toast.LENGTH_SHORT)
                    .show();
            return;
        }
        if (mEndPoint3 == null) {
            Toast.makeText(LocationActivity.this, "终点未设置", Toast.LENGTH_SHORT)
                    .show();
        }

        final RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo(
                mStartPoint, mEndPoint3);

        RouteSearch.DriveRouteQuery query = new RouteSearch.DriveRouteQuery(fromAndTo, mode, null,
                null, "");// 第一个参数表示路径规划的起点和终点，第二个参数表示驾车模式，第三个参数表示途经点，第四个参数表示避让区域，第五个参数表示避让道路
        mRouteSearch.calculateDriveRouteAsyn(query);// 异步路径规划驾车模式查询

    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }
    /**
     * 方法必须重写
     */
    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }
}
